//
//  YYT_MVPlayerViewController.m
//  AudioVisualFeast
//
//  Created by 张琪 on 16/1/28.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "QL_MVPlayerViewController.h"
#import "QL_MVModel.h"
#import "QL_MVPlayerViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "QL_MVViewViewController.h"
#import "QL_MVDescriptionTableViewCell.h"
#import "QL_CommentTableViewCell.h"
#import "QL_MVCommentModel.h"
#import "FeSpinnerTenDot.h"
#import "CUSFlashLabel.h"
#import "QL_SearchYuedanTableViewCell.h"
#import "QL_AboutMVTableViewCell.h"
#import <MJRefresh.h>
#import "QL_DataBaseSingleton.h"


@interface QL_MVPlayerViewController ()<jindu,UITableViewDelegate,UITableViewDataSource,FeSpinnerTenDotDelegate,NSURLSessionDownloadDelegate>
#pragma mark  开场动画
{
    NSInteger index;
}
// 菊花怪
@property (strong, nonatomic) FeSpinnerTenDot *spinner;
// 菊花下面跟随的文字数组
@property (strong, nonatomic) NSArray *arrTitile;
// 标题
@property (nonatomic, retain) UISegmentedControl *segment;

@property (nonatomic, retain) UILabel *titelelabel;

@property (nonatomic, retain) QL_MVModel *model;
@property (nonatomic, retain) UITableView *tableview;
@property (nonatomic, assign) NSInteger ifthree;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic, retain) NSMutableArray *commentsData;
@property (nonatomic, retain) QL_MVPlayerViewController *movie;
@property (nonatomic, retain) AVPlayerItem *playerItem;
@property (nonatomic, retain) AVPlayer *player;
@property (nonatomic, retain) AVPlayerLayer *playerLayer;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            @property (nonatomic, retain) UIView *controlAV;
//控制tabbar动画执行一次
@property (nonatomic, assign) NSInteger text_control;
//scroll坐标
@property (nonatomic, assign) NSInteger test;
//控制台显示持续时间
@property (nonatomic, assign) NSInteger secondsAV;
@property (nonatomic, retain) UIView *shijianview;
//开始暂停
@property (nonatomic, retain) UIButton *startstop;
@property (nonatomic, assign) NSInteger startstopnum;
//进度条
@property (nonatomic, retain) UISlider *jindu;
@property (nonatomic, assign) NSTimer *timer;
//视频长度
@property (nonatomic, assign) float avtime;
//时间
@property (nonatomic, assign) NSInteger fen;
@property (nonatomic, assign) NSInteger miao;
@property (nonatomic, retain) UILabel *times;
//全屏
@property (nonatomic, retain) UIButton *fullScreen;

//把下载定义成属性
@property (nonatomic,retain)NSURLSessionDownloadTask *task;
@end

@implementation QL_MVPlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
//    self.navigationController.navigationBar.translucent = NO;
#pragma mark - 动画效果下面伴随的文字
    // 定义index的值
    index = 0;
    // 开场动画下面伴随的文字数组
    self.arrTitile = [NSArray arrayWithObjects:[NSString stringWithFormat:@"即将播放:%@", self.headtitle], nil];
    _spinner = [[FeSpinnerTenDot alloc] initWithView:self.view withBlur:NO];
    _spinner.titleLabelText = _arrTitile[index];
    _spinner.delegate = self; // 协议
    [self.view addSubview:_spinner];
    
    // 初始化装评论的数组
    self.commentsData = [NSMutableArray arrayWithCapacity:0];
    // 刷新
    [self addheader];
    [self addfooter];
    
    //查询数据库里表的状态
    QL_DataBaseSingleton *dataBase = [QL_DataBaseSingleton shareDataBase];
    
    if ([dataBase selectSeeDataBase:self.ids]) {
        self.see = YES;
    }else{
        self.see = NO;
    }
    
    if (self.see == NO) {
        [[QL_DataBaseSingleton shareDataBase]insertSeeDataBase:self.ids withTitle:self.headtitle withImageurl:self.pic];
    }
}



#pragma mark - 创建视频播放器
-(void)creatav {
    // MV状态播放model.url
    NSURL *sourceMovieURL = [NSURL URLWithString:_model.url];
    // tyeup == 1属于悦单 这种状况
    if (self.typeup == 1) {
        sourceMovieURL = [NSURL URLWithString:[_model.relatedVideo[0] urls]];
        
    }
    //    使用AVPlayer來播放影片
    AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
    self.playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
    self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    self.playerLayer.frame = CGRectMake(0, 64, self.view.bounds.size.width, 200);
    [self.view.layer addSublayer:self.playerLayer];
    //  获取视频长度
    NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]
                                                     forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
    AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:opts];
    //  视频长度
    self.avtime = urlAsset.duration.value/urlAsset.duration.timescale;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timers) userInfo:nil repeats:YES];
    //  创建视频控制台
    [self controlAVview];
    //  直接播放
    [self.player play];
}
//视频实时进度
-(void)timers {
    // 视频的进度
    self.jindu.value = CMTimeGetSeconds(self.player.currentItem.currentTime) / CMTimeGetSeconds(self.player.currentItem.duration) * self.avtime;
    // 分
    NSInteger fens = self.jindu.value / 60;
    // 秒
    NSInteger miaos = self.jindu.value - fens * 60;
    self.fen = self.avtime / 60;
    self.miao = self.avtime - self.fen * 60;
    self.times.text = [NSString stringWithFormat:@"%02ld:%02ld/%02ld:%02ld",fens, miaos, self.fen, self.miao];
    // 根据控制台显示持续时间设置底部view设置透明度
    if (self.secondsAV > 0) {
        // 每次 - 1
        self.secondsAV -= 1;
        // 当等于1的时候下面控制器View
        if (self.secondsAV == 1) {
            self.controlAV.alpha = 0.0;
        }
    }
}
//视频里各种控件
-(void)controlAVview {
    self.shijianview = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 175)];
    [self.view addSubview:self.shijianview];
    //    1.点击
    UITapGestureRecognizer *tapshijianview = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shijianviewclick:)];
    //    执行方法需要点击几次
    tapshijianview.numberOfTapsRequired = 1;
    //    几个手指
    tapshijianview.numberOfTouchesRequired = 1;
    [self.shijianview addGestureRecognizer:tapshijianview];
    //    底部view
    self.controlAV = [[UIView alloc] initWithFrame:CGRectMake(0, 175 + 64, self.view.bounds.size.width, 30)];
    [self.view addSubview:self.controlAV];
    self.controlAV.backgroundColor = [UIColor blackColor];
    self.controlAV.alpha = 0.5;
    //    开始暂停
    self.startstop = [UIButton buttonWithType:UIButtonTypeSystem];
    //   设置坐标
    self.startstop.frame = CGRectMake(10, 0, 30, 30);
    //  因为刚进入这个页面 就让视频直接播放，给图标设置成暂停样式
    [self.startstop setImage:[[UIImage imageNamed:@"zanting"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    //    暂停按钮事件
    [self.startstop addTarget:self action:@selector(stopAV:) forControlEvents:UIControlEventTouchDown];
    //    放在底部View
    [self.controlAV addSubview:self.startstop];
    //    进度条坐标
    self.jindu = [[UISlider alloc] initWithFrame:CGRectMake(45, 0, self.controlAV.bounds.size.width - 170, 30)];
    //    视频进度最大值 = 视频长度
    self.jindu.maximumValue = self.avtime;
    self.jindu.minimumValue = 0.0f;
    //    进度条颜色
    self.jindu.minimumTrackTintColor = [UIColor colorWithRed:30 / 255.0 green:195 / 255.0 blue:153 / 255.0 alpha:1.0];
    //   赋图
    [self.jindu setThumbImage:[UIImage imageNamed:@"huakuai"] forState:UIControlStateNormal];
    //   进度开始 按下
    [self.jindu addTarget:self action:@selector(jindubegin:) forControlEvents:UIControlEventTouchDown];
    //    可以使用UIControlEventValueChanged方法，为基本的UIControl事件或自己的自定义事件发送通知。例如，如果你的控件值正在发生变化，就可以 发送相应通知，通过控件的代码可以指定时间目标，这个通知将被传播到这些指定的目标
    [self.jindu addTarget:self action:@selector(jindu:) forControlEvents:UIControlEventValueChanged];
    [self.jindu addTarget:self action:@selector(jinduend:) forControlEvents:UIControlEventTouchUpInside];
    [self.controlAV addSubview:self.jindu];
    //    点击进度条
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jindutapclick:)];
    //    执行方法需要点击几次
    tap.numberOfTapsRequired = 1;
    //    几个手指
    tap.numberOfTouchesRequired = 1;
    [self.jindu addGestureRecognizer:tap];
    //    时间
    self.times = [[UILabel alloc] initWithFrame:CGRectMake(self.controlAV.bounds.size.width - 120, 0, 80, 30)];
    self.times.font = [UIFont systemFontOfSize:13];
    self.times.textColor = [UIColor whiteColor];
    [self.controlAV addSubview:self.times];
    //    全屏按钮
    self.fullScreen = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.fullScreen setImage:[[UIImage imageNamed:@"quanping"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    self.fullScreen.frame = CGRectMake(self.controlAV.bounds.size.width - 35, 0, 30, 30);
    [self.fullScreen addTarget:self action:@selector(fullScreen:) forControlEvents:UIControlEventTouchDown];
    [self.controlAV addSubview:self.fullScreen];
}

//全屏键
-(void)fullScreen:(UIButton *)sender {
    [self.player pause];// 暂停
    [self.timer setFireDate:[NSDate distantFuture]];
    QL_MVViewViewController *mv = [[QL_MVViewViewController alloc]init];
    mv.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    // 属性传值
    mv.startstopnum = self.startstopnum;
    mv.titleNames = self.headtitle;
    mv.delegate = self;
    mv.url = _model.hdUrl;
    // 悦单情况
    if (self.typeup == 1) {
        mv.url = [_model.relatedVideo[0] urls];
    }
    mv.now = self.jindu.value;
    mv.howLong = self.avtime;
    [mv aaaa];
    
    [self presentViewController:mv animated:YES completion:^{
        
    }];
}
//全屏返回方法
-(void)jindunow:(NSInteger)num withIfStart:(NSInteger)ifStart {
    self.startstopnum = ifStart;
    self.jindu.value = num;
    CMTime dragedCMTime = CMTimeMake(num, 1);
    [self.player seekToTime:dragedCMTime];
    NSInteger fens = self.jindu.value / 60;
    NSInteger miaos = self.jindu.value - fens * 60;
    self.times.text = [NSString stringWithFormat:@"%02ld:%02ld/%02ld:%02ld",fens, miaos, self.fen, self.miao];
    [self.timer setFireDate:[NSDate distantPast]];
    
    if (self.startstopnum != 1) {
        [self.startstop setImage:[[UIImage imageNamed:@"zanting"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [self.player play];
    } else if (self.startstopnum == 1) {
        [self.startstop setImage:[[UIImage imageNamed:@"kaishi"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [self.player pause];
    }
}
//暂停键
-(void)stopAV:(UIButton *)sender {
    //    ！ = 1 播放状态  = 1 暂停状态
    if (self.startstopnum != 1) {
        // 播放状态下 就暂停
        [self.player pause];
        self.startstopnum = 1;
        // 改变图片
        [self.startstop setImage:[[UIImage imageNamed:@"kaishi"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        // 暂停状态
    } else if (self.startstopnum == 1) {
        // 视频就播放
        [self.player play];
        self.startstopnum = 0;
        [self.startstop setImage:[[UIImage imageNamed:@"zanting"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
    //    控制台显示持续时间赋值
    self.secondsAV = 6;
}
//按下停止
- (void)jindubegin:(UISlider *)sender {
    // 视频停止
    [self.player pause];
    //    [self.timer setFireDate:[NSDate distantFuture]];
    // 控制台显示持续时间赋值
    self.secondsAV = 6;
}
//滑动时候 改变时间
- (void)jindu:(UISlider *)sender {
    NSInteger fens = self.jindu.value / 60;
    NSInteger miaos = self.jindu.value - fens * 60;
    self.times.text = [NSString stringWithFormat:@"%02ld:%02ld/%02ld:%02ld",fens, miaos, self.fen, self.miao];
    
}
//点击结束
- (void)jinduend:(UISlider *)sender {
    CMTime dragedCMTime = CMTimeMake(self.jindu.value, 1);
    [_player seekToTime:dragedCMTime completionHandler:
     ^(BOOL finish){
         [self.timer setFireDate:[NSDate distantPast]];
         [self.player play];
     }];
    // - (void)setFireDate:(NSDate *)date;//设置fireData，其实暂停、开始会用到
    [self.timer setFireDate:[NSDate distantPast]];
    
}
//滑块点击事件
-(void)jindutapclick:(UITapGestureRecognizer *)sender {
    //    CGPoint: 表示一个二维坐标系中的点 存进度
    CGPoint point = [sender locationInView:self.jindu];
    NSInteger avlong = self.avtime;
    NSInteger width = self.controlAV.bounds.size.width - 170;
    // 位置和时间关联起来
    self.jindu.value = point.x * ((float)avlong / (float)(width));
    CMTime dragedCMTime = CMTimeMake(self.jindu.value, 1);
    [self.player seekToTime:dragedCMTime];
    //     滑动结束时间
    NSInteger fens = self.jindu.value / 60;
    NSInteger miaos = self.jindu.value - fens * 60;
    self.times.text = [NSString stringWithFormat:@"%02ld:%02ld/%02ld:%02ld",fens, miaos, self.fen, self.miao];
    [self.timer setFireDate:[NSDate distantPast]];
    [self.player play]; // 播放
    self.secondsAV = 6;
}
//点击屏幕
-(void)shijianviewclick:(UITapGestureRecognizer *)sender {
    self.controlAV.alpha = 1.0;
    // 设定持续时间 = 6
    self.secondsAV = 6;
}

// 动画效果
-(void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self start:self];
    [self performSelector:@selector(dismiss:) withObject:nil afterDelay:7.0f];
}

- (void)start:(id)sender {
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTitle) userInfo:nil repeats:YES];
    }
    [_spinner showWhileExecutingSelector:@selector(longTask) onTarget:self withObject:nil completion:^{
        [_timer invalidate];
        _timer = nil;
        index = 0;
        [self.navigationController popToRootViewControllerAnimated:YES];
    }];
}

-(void) longTask {
    // 1秒后异步执行网络请求
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 动画中 加载数据
        if (self.typeup != 1) {
            [self getData:self.ids withOffset:self.num withUrl:@"http://mapi.yinyuetai.com/video/comment/list.json?D-A=0" withcan:@"videoId"];
        } else {
            [self getData:self.ids withOffset:self.num withUrl:@"http://mapi.yinyuetai.com/playlist/comment/list.json?D-A=0" withcan:@"id"];
        }
        
    });
    sleep(6);
}

- (void)dismiss:(id)sender {
//        设置渐变题目
    CUSFlashLabel *label = [[CUSFlashLabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 150 + 64, 30)];
    [label setText:self.headtitle];
    [label setFont:[UIFont systemFontOfSize:20]];
    [label setContentMode:UIViewContentModeScaleToFill];
    label.layer.borderColor = [[UIColor clearColor]CGColor];
    [label startAnimating];
//    // 放在导航栏上
    self.navigationItem.titleView = label;
    // 添加左侧返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"MVfanhui"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(MVleft:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"MVshoucang"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(MVright:)];
    
    
    // Segment
    NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"2", @"3", nil];
    self.segment = [[UISegmentedControl alloc] initWithItems:array];
    self.segment.frame = CGRectMake(20, 210 + 64, self.view.bounds.size.width - 40, 30);
    [self.view addSubview:self.segment];
    //    设置当前选择为哪个分段
    _segment.selectedSegmentIndex = 0;
    self.ifthree = 0;
    //    判断悦单状态的segment
    if (self.typeup != 1) {
        [_segment setTitle:@"MV描述" forSegmentAtIndex:0];
        [_segment setTitle:@"评论" forSegmentAtIndex:1];
        [_segment setTitle:@"相关MV" forSegmentAtIndex:2];
    }else {
        [_segment setTitle:@"悦单描述" forSegmentAtIndex:0];
        [_segment setTitle:@"悦单评论" forSegmentAtIndex:1];
        [_segment setTitle:@"悦单列表" forSegmentAtIndex:2];
    }
    
    //    设置颜色
    _segment.tintColor = [UIColor colorWithRed:30 / 255.0 green:195 / 255.0 blue:153 / 255.0 alpha:1.0];
    [_segment addTarget:self action:@selector(segment:) forControlEvents:UIControlEventValueChanged];
    // 创建TableView
    [self CreatTableview];
    //  判断是否是悦单 请求不同的数据
    if (self.typeup == 1) {
        [self getData:self.ids withUrl:@"http://mapi.yinyuetai.com/playlist/show.json?D-A=0"
         ];
    } else {
        [self getData:self.ids withUrl:@"http://mapi.yinyuetai.com/video/show.json?D-A=0"];
    }
    
    [_spinner dismiss];
}

-(void) changeTitle {
    if (index >= _arrTitile.count)
        return;
    _spinner.titleLabelText = _arrTitile[index];
    index++;
}

#pragma mark -- TableView
-(void)CreatTableview {
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 260 + 64, self.view.bounds.size.width, self.view.bounds.size.height - 290) style:UITableViewStyleGrouped];
    _tableview.backgroundColor = [UIColor clearColor];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.bounces = NO;
    //    描述
    [_tableview registerClass:[QL_MVDescriptionTableViewCell class] forCellReuseIdentifier:@"LSZMVYueDanTableViewCellIdentifier"];
    //    评论
    [_tableview registerClass:[QL_CommentTableViewCell class] forCellReuseIdentifier:@"LSZCommentsTableViewCellIdentifier"];
    //    相关
    [_tableview registerClass:[QL_AboutMVTableViewCell class] forCellReuseIdentifier:@"LSZSearchMVTableViewCellIdentifier"];
    //    悦单情况
    [_tableview registerClass:[QL_SearchYuedanTableViewCell class] forCellReuseIdentifier:@"LSZyueDanTableViewCellIdentifier"];
    _tableview.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:self.tableview];
}
#pragma mark -- 网络请求
-(void)getData:(NSString *)ids withUrl:(NSString *)url{
    [QL_AFNTools getWithUrl:url parameter:@{@"relatedVideos":@"true", @"id":ids} httpHeader:@{@"App-Id":@"10201029",@"Device-Id":@"5fc617ae1220d0969b23eaab4c8f02b5",@"Device-V":@"QW5kcm9pZF80LjQuNF8xMDgwKjE5MjBfMTEwMDA5MDAw"} resposeType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dictionary = [[NSDictionary alloc] initWithDictionary:result];
        QL_MVModel *model = [[QL_MVModel alloc] initWithDictionary:dictionary];
        self.model = model;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self creatav];
        });
        
        [_tableview reloadData];
        
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

//评论
//mapi.yinyuetai.com/video/comment/list.json?D-A=0&v=2&offset=0&videoId=2442052&size=10
-(void)getData:(NSString *)ids withOffset:(NSInteger)offset withUrl:(NSString *)url withcan:(NSString *)can{
    [QL_AFNTools getWithUrl:url parameter:@{@"v":@"2", can:ids, @"size":@"10", @"offset":[NSString stringWithFormat:@"%ld", offset]} httpHeader:@{@"App-Id":@"10201029",@"Device-Id":@"5fc617ae1220d0969b23eaab4c8f02b5",@"Device-V":@"QW5kcm9pZF80LjQuNF8xMDgwKjE5MjBfMTEwMDA5MDAw"} resposeType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dictionary = [[NSDictionary alloc] initWithDictionary:result];
        NSArray *array = dictionary[@"comments"];
        for (NSDictionary *dic in array) {
            QL_MVCommentModel *model = [[QL_MVCommentModel alloc] initWithDictionary:dic];
            [self.commentsData addObject:model];
        }
        [_tableview reloadData];
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

#pragma mark - TableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 判断MV 描述 和悦单
    if (self.ifthree == 0) {
        if (self.typeup != 1) {
            QL_MVDescriptionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LSZMVYueDanTableViewCellIdentifier"];
            cell.backgroundColor = [UIColor clearColor];
            NSArray *array = [[NSArray alloc] initWithObjects:_model.artistName, _model.regdate, _model.totalViews, _model.totalPcViews, _model.totalMobileViews, _model.descriptions, _model.artistAvatar, nil];
            if (array.count > 0) {
                cell.array = array;
            }
            // cell 框
            cell.userInteractionEnabled = NO;
            cell.selectionStyle = UITableViewCellSelectionStyleNone; // 不让cell被点击
            cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
            return cell;
        }
        else {
//            // 悦单的情况
            QL_SearchYuedanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LSZyueDanTableViewCellIdentifier"];
            cell.backgroundColor = [UIColor clearColor];
            NSArray *array = [[NSArray alloc] initWithObjects:self.headtitle, _model.updateTime, _model.totalViews, _model.totalFavorites, _model.integral, _model.nickName,_model.descriptions, nil];
            if (array.count > 2) {
                cell.array = array;
            }
            return cell;
        }
        // 点击评论 显示评论的cell
    } else if (self.ifthree == 1) {
        
        QL_CommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LSZCommentsTableViewCellIdentifier"];
        if (self.commentsData.count > 0) {
            cell.model = self.commentsData[indexPath.row];
        }
        cell.backgroundColor = [UIColor clearColor];
        return cell;
        // 显示相关MV的cell
    } else if (self.ifthree == 2) {
        QL_AboutMVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LSZSearchMVTableViewCellIdentifier"];
//        cell.model = _model.relatedVideo[indexPath.row];
        cell.modelShared = _model.relatedVideo[indexPath.row];
        cell.title.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor clearColor];
        return cell;
        
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.ifthree == 2) {
        //    设置渐变题目
        CUSFlashLabel *label = [[CUSFlashLabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 50 , 30)];
        [label setText:[_model.relatedVideo[indexPath.row] title]];
        [label setFont:[UIFont systemFontOfSize:18]];
        [label setContentMode:UIViewContentModeScaleToFill];
        label.layer.borderColor = [[UIColor clearColor]CGColor];
        [label startAnimating];
        // 放在导航栏上
        self.navigationItem.titleView = label;
        self.ids = [_model.relatedVideo[indexPath.row] ids];
        [self.playerLayer removeFromSuperlayer];
        [self.shijianview removeFromSuperview];
        [self.fullScreen removeFromSuperview];
        [self.times removeFromSuperview];
        [self.jindu removeFromSuperview];
        [self.startstop removeFromSuperview];
        [self.shijianview removeFromSuperview];
        [self.controlAV removeFromSuperview];
        [self.playerLayer removeFromSuperlayer];
        if (self.typeup == 1) {
            [self getData:self.ids withUrl:@"http://mapi.yinyuetai.com/playlist/show.json?D-A=0"];
            
            
        } else {
            [self getData:self.ids withUrl:@"http://mapi.yinyuetai.com/video/show.json?D-A=0"];
            
            
        }
        
    }}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
// 返回个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.ifthree == 0) {
        return 1;
    } else if (self.ifthree == 1) {
        return self.commentsData.count;
    } else if (self.ifthree == 2) {
        return 10;
    }
    return 0;
}
// 返回高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.ifthree == 0) {
        return 250;
    } else if (self.ifthree == 1) {
        return 110;
    } else if (self.ifthree == 2) {
        return 100;
    }
    return 500;
}

#pragma mark -- segment
-(void)segment:(UISegmentedControl *)sender {
    if (sender.selectedSegmentIndex == 0) {
        self.ifthree = 0;
        _tableview.bounces = NO;
        _tableview.contentOffset = CGPointMake(0, 0);
        [_tableview reloadData];
    } else if (sender.selectedSegmentIndex == 1) {
        self.ifthree = 1;
        _tableview.bounces = YES;
        _tableview.contentOffset = CGPointMake(0, 0);
        [self.commentsData removeAllObjects];
        if (self.typeup != 1) {
            [self getData:self.ids withOffset:self.num withUrl:@"http://mapi.yinyuetai.com/video/comment/list.json?D-A=0" withcan:@"videoId"];
        } else {
            [self getData:self.ids withOffset:self.num withUrl:@"http://mapi.yinyuetai.com/playlist/comment/list.json?D-A=0" withcan:@"id"];
        }
        [_tableview reloadData];
    } else if (sender.selectedSegmentIndex == 2) {
        self.ifthree = 2;
        _tableview.bounces = NO;
        _tableview.contentOffset = CGPointMake(0, 0);
        [_tableview reloadData];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 左上角返回之后让暂停
- (void)MVleft:(UIButton *)sender {
    [self.player pause];
    [self.navigationController popViewControllerAnimated:YES];
    [self.playerLayer removeFromSuperlayer];
    [self.shijianview removeFromSuperview];
    [self.fullScreen removeFromSuperview];
    [self.times removeFromSuperview];
    [self.jindu removeFromSuperview];
    [self.startstop removeFromSuperview];
    [self.controlAV removeFromSuperview];
}

- (void)MVright:(UIButton *)btn{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@""]];
    NSURLSessionConfiguration *configur = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configur delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    
    self.task = [session downloadTaskWithRequest:request];
}



#pragma mark -- 上拉刷新
-(void)addfooter {
    self.tableview.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)1.0*NSEC_PER_SEC), dispatch_get_main_queue(), ^{
            self.num += 10;
            if (self.typeup != 1) {
                [self getData:self.ids withOffset:self.num withUrl:@"http://mapi.yinyuetai.com/video/comment/list.json?D-A=0" withcan:@"videoId"];
            } else {
                [self getData:self.ids withOffset:self.num withUrl:@"http://mapi.yinyuetai.com/playlist/comment/list.json?D-A=0" withcan:@"id"];
            }

            
        });
        
    }];
}
#pragma mark -- 下拉刷新
-(void)addheader {
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.num = 0;
            [self.commentsData removeAllObjects];
            if (self.typeup != 1) {
                [self getData:self.ids withOffset:self.num withUrl:@"http://mapi.yinyuetai.com/video/comment/list.json?D-A=0" withcan:@"videoId"];
            } else {
                [self getData:self.ids withOffset:self.num withUrl:@"http://mapi.yinyuetai.com/playlist/comment/list.json?D-A=0" withcan:@"id"];
            }

        });
        
    }];
    [self.tableview.mj_header beginRefreshing];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
